home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / dualhead / dualhead.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.0 KB  |  348 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * dh.c
  19.  *  
  20.  *    this code puts up a window with 3 buttons.  with these buttons,
  21.  *    you can create X or GL windows.  if the envrionmental variable
  22.  *    ALT_DISPLAY is set, you can create the windows on another display
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <device.h>
  27. #include <math.h>
  28. #include <X11/Intrinsic.h>
  29. #include <X11/IntrinsicP.h>
  30. #include <X11/CoreP.h>
  31. #include <X11/Xirisw/GlxMDraw.h>
  32. #include <X11/Shell.h>
  33. #include <Xm/BulletinB.h>
  34. #include <Xm/Form.h>
  35. #include <Xm/RowColumn.h>
  36. #include <Xm/PushB.h>
  37.  
  38. #include <gl/glws.h>
  39.  
  40.  
  41. /*
  42.  * GLXconfig for a doublebuffered, RGB gl drawing area
  43.  */
  44. static GLXconfig view_glxconfig [] = {
  45.     { GLX_NORMAL, GLX_DOUBLE, TRUE },
  46.     { GLX_NORMAL, GLX_RGB, TRUE },
  47.     { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
  48.     { 0, 0, 0 }
  49. };
  50.  
  51. /*
  52.  * some colors
  53.  */
  54. #define COLOR1 0x00FF1111
  55. #define COLOR2 0x000000FF
  56.  
  57. /*
  58.  * some X colors
  59.  */
  60. int WHITE_PIXEL_NUM;
  61. int BLACK_PIXEL_NUM;
  62.  
  63. /*
  64.  * global X stuff
  65.  */
  66.  
  67. Widget toplevel, toplevel2, toplevel3;
  68.  
  69. /*
  70.  * X display structure pointers;  
  71.  */
  72. Display  *main_display;           /* display #1, the main display */
  73. Display  *alt_display ;           /* display #2, the alternate display */
  74. Display  *current_display;      /* the current display */
  75.  
  76. XtAppContext appContext;  /* X application context */
  77.  
  78. long curr_color;         /* current selected gl color */
  79. int curr_X_color;        /* current selected X color */
  80.  
  81.  
  82. /*
  83.  * some callback functions
  84.  */
  85. void initGl1();
  86. void newGL(); 
  87. void newX(); 
  88. void switchS();
  89.  
  90. /*
  91.  * main routine
  92.  */
  93. main(argc, argv) 
  94.     int   argc;
  95.     char *argv[];
  96. {
  97.  
  98.    /*
  99.     * local variables 
  100.     */
  101.     Arg       args[10];
  102.     Cardinal       n;
  103.     int       i = 0;
  104.     Widget     f1, f2, f3;
  105.     Widget     b1, b2, b3;
  106.   
  107.     char        *screen1, *screen2;
  108.     char        *alternate_display;
  109.  
  110.  
  111. /*
  112.  * initialize toolkit, open display
  113.  */
  114.   XtToolkitInitialize();
  115.   appContext = XtCreateApplicationContext();
  116.   main_display = XtOpenDisplay(appContext, ":0.0", NULL, "Explore",
  117.                  NULL, 0, &argc, argv);
  118.   if (!main_display) {
  119.       printf("\n can't open display");
  120.       exit(1);
  121.       }
  122.  
  123.  
  124. /*
  125.  * create a top level shell to hold the control buttons
  126.  */
  127.   n = 0;
  128.   toplevel = XtAppCreateShell(NULL, "dh",
  129.                    applicationShellWidgetClass, main_display, NULL, 0);
  130.  
  131.   n = 0;
  132.   XtSetArg(args[n], XmNheight, 200); n++;
  133.   XtSetArg(args[n], XmNwidth, 100); n++;
  134.   f1 = XmCreateRowColumn(toplevel, "form", args, n);
  135.   XtManageChild(f1);
  136.  
  137.   n = 0;
  138.   b1 = XmCreatePushButton(f1, "newGL", args, n);
  139.   XtManageChild(b1);
  140.   XtAddCallback(b1, XmNactivateCallback, newGL, 0);
  141.  
  142.   n = 0;
  143.   b2 = XmCreatePushButton(f1, "newX", args, n);
  144.   XtManageChild(b2);
  145.   XtAddCallback(b2, XmNactivateCallback, newX, 0);
  146.  
  147.   n = 0;
  148.   b3 = XmCreatePushButton(f1, "switch screen", args, n);
  149.   XtManageChild(b3);
  150.   XtAddCallback(b3, XmNactivateCallback, switchS, 0);
  151.  
  152.   XtRealizeWidget(toplevel);
  153.  
  154. /*
  155.  * now get the ALT_DISPLAY environment variable.  if it is not set,
  156.  * display a message and run the alternate display on the same screen
  157.  * as the main display
  158.  */
  159.   alternate_display = (char *) getenv("ALT_DISPLAY");
  160.  
  161.   if (alternate_display == NULL)
  162.       fprintf(stderr, "\nALT_DISPLAY not set, running single display\n");
  163.  
  164.   if ((alt_display  = XtOpenDisplay(appContext, alternate_display, NULL, "Explore",
  165.        NULL, 0, &argc, argv)) == NULL) {
  166.        printf("\n can't open second display, running single...");
  167.        fflush(stdout);
  168.        alt_display  = main_display;
  169.        }
  170.  
  171.  /*
  172.   * set the current display and current color
  173.   */
  174.   current_display = alt_display ; 
  175.   curr_color = COLOR2;
  176.  
  177.  /*
  178.   * get the white pixel value for the main display
  179.   */
  180.   WHITE_PIXEL_NUM = WhitePixel(main_display, 0);
  181.  
  182.  /*
  183.   * get the black pixel value for the alternate display
  184.   */
  185.   BLACK_PIXEL_NUM = BlackPixel(alt_display, 0);
  186.  
  187.   curr_X_color = BLACK_PIXEL_NUM;
  188.  
  189.  /*
  190.   * X main loop
  191.   */
  192.   XtAppMainLoop(appContext);
  193.  
  194. }
  195.  
  196.  
  197. /*
  198.  * callback routine to initialize a GL window;  it just
  199.  * clears the gl drawing area with the current color
  200.  */
  201. void initGl1(w, calldata, clientdata)
  202.  
  203. Widget w;
  204. char *calldata, *clientdata;
  205. {
  206.  
  207.    GLXwinset(XtDisplay(w), XtWindow(w));
  208.  
  209.    cpack(curr_color);
  210.    clear();
  211.    swapbuffers();
  212. }
  213.  
  214.  
  215. /*
  216.  * callback for the new GL button.  it  creates a new shell
  217.  * with a gl drawing area in it.  the shell is created on the
  218.  * current display, via the "current_display" argument int the
  219.  * XtAppCreateShell() call
  220.  */  
  221. void newGL(w, calldata, clientdata)
  222.  
  223. Widget w;
  224. char *calldata, clientdata;
  225. {
  226.  
  227.     Widget top, gl;
  228.     int n;
  229.     Arg  args[10];
  230.     XmString    xmstr;
  231.     char        window_name[128];
  232.  
  233.  /*
  234.   * we will set the window name to be:
  235.   *     "newGL <display name>"
  236.   * so for screen :0.0 it would be:
  237.   *    "newGL :0.0"
  238.   * we can get the display name from the XDisplay structure member
  239.   * "display_name"
  240.   */
  241.   memset(window_name, 0, 128);
  242.   strcpy(window_name, "newGL ");
  243.   strcat(window_name, DisplayString(current_display));
  244.  
  245.  /*
  246.   * creat the new shell window 
  247.   */
  248.   n = 0;
  249.   top = XtAppCreateShell(window_name, "newGL",
  250.                    applicationShellWidgetClass, current_display, NULL, 0);
  251.  
  252.  /*
  253.   * create the gl drawing area, add appropriate callbacks
  254.   */
  255.   n = 0;
  256.   XtSetArg(args[n], XmNheight, 200); n++;
  257.   XtSetArg(args[n], XmNwidth, 200); n++;
  258.   XtSetArg(args[n], GlxNglxConfig, view_glxconfig); n++;
  259.   gl = GlxCreateMDraw(top, "view", args, n);
  260.   XtManageChild(gl);
  261.   XtAddCallback(gl, GlxNginitCallback, (XtCallbackProc)initGl1, NULL);
  262.   XtAddCallback(gl, GlxNexposeCallback, (XtCallbackProc)initGl1, NULL);
  263.  
  264.  /*
  265.   * realize widget
  266.   */
  267.   XtRealizeWidget(top);
  268.  
  269. }
  270.  
  271.  
  272. /*
  273.  * callback for the new X button.  it  creates a new application shell
  274.  * with a row/column widget in it.  the shell is created on the
  275.  * current display, via the "current_display" argument int the
  276.  * XtAppCreateShell() call
  277.  */
  278. void newX(w, calld, clientd)
  279.  
  280. Widget w;
  281. char *calld, *clientd;
  282.  
  283. {
  284.     Widget   top, f;
  285.     int      n;
  286.     Arg      args[10];
  287.     char     window_name[128];
  288.  
  289.  /*
  290.   * we will set the window name to be:
  291.   *      "newX <display name>"
  292.   * so for screen :0.0 it would be:
  293.   *      "newX :0.0"
  294.   * we can get the display name from the XDisplay structure member
  295.   * "display_name"
  296.   */
  297.   memset(window_name, 0, 128);
  298.   strcpy(window_name, "newX ");
  299.   strcat(window_name, DisplayString(current_display));
  300.  
  301.  /*
  302.   * create the new shell
  303.   */
  304.   n = 0;
  305.   XtSetArg(args[n], XmNheight, 200); n++;
  306.   XtSetArg(args[n], XmNwidth, 200); n++;
  307.   XtSetArg(args[n], XmNbackground, curr_X_color); n++;
  308.   top = XtAppCreateShell(window_name, "newX",
  309.                    applicationShellWidgetClass, current_display, args, n);
  310.  
  311.  /*
  312.   * realize the widget 
  313.   */
  314.   XtRealizeWidget(top);
  315. }
  316.  
  317.  
  318. /*
  319.  * switch display button callback.  it toggles between displays and
  320.  * the current gl fill color
  321.  */
  322. void switchS(w, calld, clid)
  323.  
  324. Widget w;
  325. char *calld, clid;
  326. {
  327.  
  328.   /*
  329.    * if the current display is the main display, main_display,
  330.    * switch to the alternate display, alt_display , and switch colors
  331.    */
  332.    if (current_display == main_display) {
  333.        current_display = alt_display ;
  334.        curr_color = COLOR2;
  335.        curr_X_color = BLACK_PIXEL_NUM;
  336.        }
  337.    else {
  338.  
  339.      /*
  340.       * alternate display is current, so switch back to the main
  341.       * display, main_display, and switch colors
  342.       */
  343.        current_display = main_display;
  344.        curr_color = COLOR1;
  345.        curr_X_color = WHITE_PIXEL_NUM;
  346.        }
  347. }
  348.